//
//  PhotoUntil.m
//  UkongM_1.7
//
//  Created by ukongm on 16/8/9.
//  Copyright © 2016年 ukongm. All rights reserved.
//

#import "PhotoUntil.h"

@interface PhotoUntil()

@property(nonatomic)UIViewController<PhotoUntilDelegate>* tager;

@end

@implementation PhotoUntil


+ (instancetype)sharedWithTager:(UIViewController <PhotoUntilDelegate>*)tager
{
    static PhotoUntil* until;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        until = [PhotoUntil new];
    });
    
    until.tager = tager;
    
    return until;
}

- (void)openCamera
{
    [self openCameraWithEdit:YES];
    
}

- (void)openCameraWithEdit:(BOOL)isEdit{
    
    
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    
    picker.delegate = self;
    
    if (isEdit) {
        picker.allowsEditing = YES;//设置可编辑
    }
    
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self.tager presentViewController:picker animated:YES completion:nil];//进入照相界面
}

- (void)openPhotoList
{
    
    [self openPhotoListWithEdit:YES];
}

- (void)openPhotoListWithEdit:(BOOL)isEdit{
    
    UIImagePickerController* picker = [[UIImagePickerController alloc] init];
    
    picker.delegate = self;
    
    
    if (isEdit) {
        picker.allowsEditing = YES;//设置可编辑
    }
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self.tager presentViewController:picker animated:YES completion:nil];//进入照相界面
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage* image = info[UIImagePickerControllerEditedImage];
    
    if (image == nil) {
        image = info[UIImagePickerControllerOriginalImage];
    }
    
    [self.tager imageWithImage:image];
}

@end
